package com.nuoniu.sibanyun.service.crm.call.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.crm.CrmGoodsOrder;
import com.nuoniu.sibanyun.entity.crm.bo.GoodsOrderDetailBo;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallBaseSetting;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallBaseSettingInfo;
import com.nuoniu.sibanyun.entity.crm.call.dto.CrmCallBaseSettingInfoSearchDto;
import com.nuoniu.sibanyun.entity.expenses.ExpensesBusinessOrder;
import com.nuoniu.sibanyun.mapper.crm.call.CrmCallBaseSettingInfoMapper;
import com.nuoniu.sibanyun.service.crm.ICrmGoodsCallSettingService;
import com.nuoniu.sibanyun.service.crm.ICrmGoodsService;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallBaseSettingInfoService;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallBaseSettingService;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallBillService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 客户管理-外呼套餐基础信息 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2022-04-28
 */
@Service
@Log4j2
public class CrmCallBaseSettingInfoServiceImpl extends ServiceImpl<CrmCallBaseSettingInfoMapper, CrmCallBaseSettingInfo> implements ICrmCallBaseSettingInfoService {

    @Autowired
    @Lazy
    private ICrmCallBaseSettingService baseSettingService;
    @Autowired
    @Lazy
    private ICrmCallBillService crmCallBillService;
    @Autowired
    @Lazy
    private ICrmGoodsCallSettingService goodsCallSettingService;
    @Autowired
    @Lazy
    private ICrmGoodsService goodsService;

    @Override
    public void updateFromCrmGoodsOrder(CrmGoodsOrder crmGoodsOrder) {
        LocalDateTime now = LocalDateTime.now();
        if(null == crmGoodsOrder)throw new NuoNiuException("参数错误");
        Integer orderStatus = crmGoodsOrder.getOrderStatus();
        if(!orderStatus.equals(1))return;
        Integer companyId = crmGoodsOrder.getCompanyId();
        String goodsDetail = crmGoodsOrder.getGoodsDetail();
        GoodsOrderDetailBo goodsOrderDetailBo = null;
        if(StringUtils.isNotBlank(goodsDetail)){
            goodsOrderDetailBo = JSON.parseObject(goodsDetail, GoodsOrderDetailBo.class);
        }
        if(null == goodsOrderDetailBo)throw new NuoNiuException("参数错误");
        String goodsType = goodsOrderDetailBo.getGoodsType();
        if(!"telCharges".equals(goodsType))throw new NuoNiuException("参数错误");
        CrmCallBaseSettingInfo baseSettingInfo = this.getByCompany(companyId);
        if(null != baseSettingInfo){
            LocalDateTime endTime = baseSettingInfo.getEndTime();
            if(endTime.compareTo(now) > 0)throw new NuoNiuException("套餐有效期未结束");
        }else{
            baseSettingInfo = new CrmCallBaseSettingInfo();
            baseSettingInfo.setCompanyId(companyId);
        }
        baseSettingInfo.setCrmGoodsId(goodsOrderDetailBo.getId().intValue());
        baseSettingInfo.setCrmGoodsName(goodsOrderDetailBo.getGoodsName());
        baseSettingInfo.setPrice(goodsOrderDetailBo.getMinutePrice());
        baseSettingInfo.setBaseCharges(crmGoodsOrder.getSalePrice());
        baseSettingInfo.setBaseMinutes(goodsOrderDetailBo.getBaseMinutes());
        baseSettingInfo.setStatus(1);
        baseSettingInfo.setRenewalType(2); //续费方式 1：手动 2：自动
        if(null == baseSettingInfo.getCreateTime())baseSettingInfo.setCreateTime(now);
        this.saveOrUpdate(baseSettingInfo);
        // 校验当月外呼账单，如果没有就创建账单
        crmCallBillService.checkCurrentBillFromBaseSettingInfo(baseSettingInfo);
    }

    @Override
    public CrmCallBaseSettingInfo getByCompany(Integer companyId) {
        UserInfo userInfo = UserThreadLocal.get();
        if(null == companyId){
            companyId = userInfo.getCompanyId();
        }
        QueryWrapper<CrmCallBaseSettingInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        queryWrapper.last("LIMIT 1");
        return getOne(queryWrapper,false);
    }

    @Override
    public Page<CrmCallBaseSettingInfo> pageList(CrmCallBaseSettingInfoSearchDto settingInfoSearchDto) {
        Page<CrmCallBaseSettingInfo> crmCallBaseSettingInfoPage = baseMapper.pageList(settingInfoSearchDto.getPage(), settingInfoSearchDto);
        return crmCallBaseSettingInfoPage;
    }



}
